package ui.activity.home.presenter;


import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import Bean.EnterpriseassBean;
import Bean.EnterpriseassParamBean;
import base.BaseBiz;
import dao.HomeNewsBean;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.home.biz.EnterPriseassistanceBiz;
import ui.activity.home.contract.EnterPriseassistanceContract;

/**
 * Created by chenjian on 19/2/26
 */

public class EnterPriseassistancePresenter implements EnterPriseassistanceContract.Presenter {

    EnterPriseassistanceContract.View view;
    EnterPriseassistanceBiz enterPriseassistanceBiz;
    List<HomeNewsBean> list=new ArrayList<>();


    @Inject
    public EnterPriseassistancePresenter(EnterPriseassistanceContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        enterPriseassistanceBiz = (EnterPriseassistanceBiz) biz;
    }


    @Override
    public void getNewsList(boolean isRefresh, String customId) {
        if(isRefresh){
            list.clear();
        }
        view.loading(true);
        EnterpriseassParamBean e = new EnterpriseassParamBean();
        e.setCustomId(Integer.parseInt(customId));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(e));
        enterPriseassistanceBiz.getNews(requestBody, new BaseBiz.Callback<EnterpriseassBean.DataBean>() {
            @Override
            public void onSuccess(EnterpriseassBean.DataBean info) {
                list.addAll(info.getItems());
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(EnterpriseassBean.DataBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });

    }

}
